<template>
  <div id="role_id">
    <div class="role_top">
      <div></div>  
      <div class="role_add">
        <el-button size="mini" round @click="openPanel">增加</el-button>
      </div>
    </div>
    <el-table
      :data="role_list"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="rolename"
        label="角色名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="status"
        label="角色状态" v-slot="hhh">
        <template>
            <el-button type="success" size="mini" round v-if="hhh.row.status === 1">正常</el-button>
            <el-button type="danger" size="mini" round v-else-if="hhh.row.status === 2">禁用</el-button>
        </template>    
      </el-table-column>
      <el-table-column
        label="操作" v-slot="$props">
        <template>
            <el-button type="warning" size="mini" round @click="updateRole($props.row)">修改</el-button>
        </template>
        <template>
            <el-button type="danger" size="mini" round @click="delRole($props.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <addRole ref="add_role_ref" :isShow="isShow" @reload_role="reload_role"></addRole>
  </div>
</template>

<script>
import {mapActions, mapState} from 'vuex'
import addRole from '../components/addRole.vue'
export default {
    data(){
        return {
            isShow: {flag: false}
        }
    },
    created(){
        this.RoleActions()
    },
    methods:{
        updateRole(obj){
            //打开模态框
            this.isShow.flag = true
            //设置数据
            this.$refs.add_role_ref.setForm(obj)
        },
        delRole(id){
            this.$http.post("/api/roledelete",{id}).then(
                res=>{
                    if(res.code == 200){
                        this.RoleActions()
                        alert("嘿嘿嘿，删了啊")
                    }
                }
            )
        },
        reload_role(){
            this.RoleActions()
        },
        openPanel(){
            this.isShow.flag = true
        },
        ...mapActions("RoleStore",['RoleActions'])
    },
    computed:{
        ...mapState({'role_list': state=>state.RoleStore.role_list})
    },
    components:{
        addRole
    }
};
</script>

<style>
#role_id .role_top {
  display: flex;
  justify-content: space-between;
}
#role_id .el-table{
    margin-top: 20px;
}
</style>